package com.jin.api.v1;

import com.jin.Exception.CreateSuccess;
import com.jin.Exception.http.ParameterException;
import com.jin.core.LocalUser;
import com.jin.core.enumeration.CouponStatus;
import com.jin.core.interceptors.ScopeLevel;
import com.jin.model.Coupon;
import com.jin.model.User;
import com.jin.service.CouponService;
import com.jin.vo.CouponCategoryVO;
import com.jin.vo.CouponPureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("coupon")
public class CouponController {
    @Autowired
    CouponService couponService;
    //通过一个分类的id号查看可用那些优惠券,因为商品也在分类的下面
    @GetMapping("/by/category/{cid}")
    public List<CouponPureVo> getCouponListByCategory(@PathVariable Long cid){
        List<Coupon> couponList = couponService.getByCategory(cid);
        if (couponList.isEmpty()){
            return Collections.emptyList();
        }
        List<CouponPureVo> couponPureVoList = CouponPureVo.getList(couponList);
        return couponPureVoList;
    }

    //获取全场券
    @GetMapping("/whole_store")
    public List<CouponPureVo> getWholeStoreCouponList(){
        List<Coupon> couponList = couponService.getWholeStoreCoupon();
        if (couponList.isEmpty()){
            return Collections.emptyList();
        }
        List<CouponPureVo> couponPureVoList = CouponPureVo.getList(couponList);
        return couponPureVoList;
    }

    @ScopeLevel()
    //用户领取优惠券
    @PostMapping("/collect/{id}")
    //传入一个优惠券的id值
    public void collectCoupon(@PathVariable Long id){
        Long uid = LocalUser.getUser().getId();
        couponService.collectOneCoupon(uid,id);
        //对新增的数据返回到前端不是太有用
        //设置的信息码是0表示ok
        throw new CreateSuccess(0);
    }

    @ScopeLevel()
    //根据优惠券状态查询
    @GetMapping("/myself/by/status/{status}")
    public List<CouponPureVo> getMyCouponByStatus(@PathVariable Long status){
        //得到我
        Long uid = LocalUser.getUser().getId();
        List<Coupon> couponList;

        //触发机制
        switch (CouponStatus.toType(status)){
            case AVAILABLE:
                couponList = couponService.getMyAvailableCoupons(uid);
                break;
            case USED:
                couponList = couponService.getMyUsedCoupons(uid);
                break;
            case EXPIRED:
                couponList = couponService.getMyExpiredCoupons(uid);
                break;
            default:
                throw new ParameterException(40001);
        }
        return CouponPureVo.getList(couponList);
    }

    @ScopeLevel()
    @GetMapping("/myself/available/with_category")
    public List<CouponCategoryVO> getUserCouponWithCategory() {
        User user = LocalUser.getUser();
        List<Coupon> coupons = couponService.getMyAvailableCoupons(user.getId());
        if (coupons.isEmpty()) {
            return Collections.emptyList();
        }
        return coupons.stream().map(coupon -> {
            CouponCategoryVO vo = new CouponCategoryVO(coupon);
            return vo;
        }).collect(Collectors.toList());
    }
}


